from django.test import TestCase
from rest_framework.test import APIClient
from django.contrib.auth.models import User
from info_sys.models import Buyer, Supplier, Item, OrderItem, Order
from helpme.settings import BUYER_CONTENT_TEMP, SUPPLIER_CONTENT_TEMP, MEDIA_ROOT
from datetime import datetime,timedelta
import json
# Create your tests here.
LOCAL = 'http://172.0.0.1:8000'

Remote = 'http://fs.foshanplus.com:8082'


class UserModelTests(TestCase):

    def setUp(self):
        print("=======beging UserModelTests =======")
        self.user_name = 'test1'
        self.user_phone = '13874458744'

        self.user1 = Buyer(
            phone=self.user_phone,
        )

    def test_user_api_add(self):
        res = self.client.post(
            '/buyers/',
            {
                'phone': self.user_phone,
                'password': self.user_name,
            }
        )
        result = res.json()
        # print(result)
        u = User.objects.get(id=1)
        self.assertEqual(u.username, f'buyer_{self.user_phone}')

    def test_supplier_api_add(self):
        res = self.client.post(
            '/suppliers/',
            {
                'phone': self.user_phone,
                'password': self.user_name,
            }
        )
        result = res.json()
        print(result)

        u = User.objects.get(id=1)
        self.assertEqual(u.username, f'supplier_{self.user_phone}')


class OrderModelTests(TestCase):

    def setUp(self):
        print("=======beging OrderModelTests =======")
        self.user = User.objects.create_user(
            username = 'test',
            password = '123'
        )
        self.user_phone = '13874458744'
        self.buyer = Buyer(
            user=self.user,
            phone=self.user_phone,
            city='foshan',
            zone='nanhai',
            address='test address'
        )
        self.buyer.save()
        self.item_name = 'apple'
        self.item_content= 'nice apple'


    def test_order_add(self):
        # 添加商品
        res = self.client.post(
            '/items/',
            {
                'name': self.item_name,
                'content': self.item_content
            }
        )
        new_item = res.json()
        # print(new_item)

        self.assertEqual(new_item['name'], self.item_name)

        print(json.dumps(BUYER_CONTENT_TEMP))
        # 添加订单
        res = self.client.post(
            '/orders/',
            {
                'buyer_id': self.buyer.id,
                'content': json.dumps(BUYER_CONTENT_TEMP),
                'items': json.dumps([{'id':1, 'count':10}])
            }
        )
        result = res.json()
        # print(result)

        self.assertEqual(result['buyer_phone'], self.buyer.phone)


class GoodModelTests(TestCase):

    def setUp(self):
        print("=======beging GoodModelTests =======")
        self.user = User.objects.create_user(
            username = 'test',
            password = '123'
        )
        self.user_phone = '13874458744'
        self.supplier = Supplier(
            user=self.user,
            company_name='Big house',
            phone=self.user_phone,
            city='foshan',
            zone='nanhai',
            address='test address'
        )
        self.supplier.save()
        self.good_name = 'apple'
        self.good_content= 'nice apple'

    def test_good_add(self):
        # 添加商品
        res = self.client.post(
            '/goods/',
            {
                'supplier_id': self.supplier.id,
                'name': self.good_name,
                'content': self.good_content
            },
            files = {'img': ('test.jpg', open('/home/louis/Desktop/1.png', 'rb'), 'image/png')}
        )
        new_item = res.json()
        print(new_item)

        # 添加图片

        res = self.client.post(
            '/goods/upload_img/',
            {
                'good_id': new_item['id'],
            },
            files={'img': ('test.jpg', open('/home/louis/Desktop/1.png','rb'), 'image/png')}
        )

        print(res)
